package com.hoshiicloud.elasticsearch.controller;


import com.hoshiicloud.elasticsearch.entity.Sku;
import com.hoshiicloud.elasticsearch.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller
 */
@Controller
@Api(tags = "SkuController", description = "搜索商品管理")
@RequestMapping("/sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @RequestMapping(value = "/importSkuAll", method = RequestMethod.POST)
    @ResponseBody
    public Integer importSkuAllList() {
        int count = skuService.importSkuAll();
        return count;
    }

    @ApiOperation(value = "根据id删除商品")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void delete(@PathVariable Long id) {
        skuService.delete(id);
    }

    @ApiOperation(value = "根据id批量删除商品")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public void delete(@RequestParam("ids") List<Long> ids) {
        skuService.delete(ids);
    }

    @ApiOperation(value = "根据id创建商品")
    @RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean create(@PathVariable Long id) {
        Sku sku = skuService.create(id);
        if (sku != null) {
            return true;
        } else {
            return false;
        }
    }

    @ApiOperation(value = "简单搜索")
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public Page<Sku> search(@RequestParam(required = false) String keyword,
                            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<Sku> spuPage = skuService.search(keyword, pageNum, pageSize);
        return spuPage;
    }


}
